package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockTransferIn;
import com.zt.rongda.stock.service.StockTransferInService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/stockTransferIn")
public class StockTransferInController {

    @Autowired
    private StockTransferInService stockTransferInService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockTransferInService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferInService.add(getStockTransferIn(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferInService.update(getStockTransferIn(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockTransferInService.delete(getStockTransferIn(reqBean));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockTransferIn getStockTransferIn(ReqBean reqBean) {
        StockTransferIn stockTransferIn = new StockTransferIn();
        stockTransferIn.setListId(reqBean.getInfo("listId"));
        stockTransferIn.setStockId(reqBean.getInfo("stockId"));
        stockTransferIn.setStockName(reqBean.getInfo("stockName"));
        stockTransferIn.setTypeId(reqBean.getInfo("typeId"));
        stockTransferIn.setTypeName(reqBean.getInfo("typeName"));
        stockTransferIn.setSpec(reqBean.getInfo("spec"));
        stockTransferIn.setBrand(reqBean.getInfo("brand"));
        stockTransferIn.setUnit(reqBean.getInfo("unit"));
        stockTransferIn.setNum(reqBean.getInfoInt("num"));
        stockTransferIn.setPrice(reqBean.getInfo("price"));
        stockTransferIn.setTotalPrice(reqBean.getInfo("totalPrice"));
        stockTransferIn.setStockDate(reqBean.getInfoDate("stockDate"));
        stockTransferIn.setLocation(reqBean.getInfo("location"));
        stockTransferIn.setSupplierId(reqBean.getInfo("supplierId"));
        stockTransferIn.setSupplierName(reqBean.getInfo("supplierName"));
        stockTransferIn.setSettlement(reqBean.getInfo("settlement"));
        stockTransferIn.setPurpose(reqBean.getInfo("purpose"));
        stockTransferIn.setContent(reqBean.getInfo("content"));
        stockTransferIn.setUserId(reqBean.getInfo("userId"));
        stockTransferIn.setUserName(reqBean.getInfo("userName"));
        stockTransferIn.setPurchaseListId(reqBean.getInfo("purchaseListId"));
        stockTransferIn.setType(reqBean.getInfo("type"));
        stockTransferIn.setOperator(reqBean.getUserId());
        stockTransferIn.setRemark(reqBean.getInfo("remark"));
        return stockTransferIn;
    }

}